const fs = require('fs')
const express = require('express')
const router = express.Router()
const service = require('../service/userTelService')
const Joi = require('joi')
const PATH = '/userTel'

/* router.get('/queryAll', function (req, res) {
     service.queryAll().then((data)=>{
     res.json(data)A
     })
}) */

router.post('/queryDemand', async function (req, res) {
  const schema = Joi.object({
    pageNo: Joi.number().required().label('页数')
  })
  try {
    await schema.validateAsync(req.body)
    Promise.all([service.queryDemand(req.body.pageNo, 2), service.queryCount()]).then(function (result) {
      const data = result[0]
      data.totalPage = Math.ceil(result[1].sl / 2)
      data.total = result[1].sl
      res.json(data)
    })
  } catch (err) {
    console.log(err)
  }
})
router.post('/fileLoad', async function (req, res) {
  const fileUrl = req.body.url
  fs.readFile(fileUrl, 'utf-8', (err, data) => {
    res.json(err || data)
  })
})
/* router.get('/queryDemand', function (req, res) {
    let pageNo=Number(req.query.pageNo)
    service.queryDemand(pageNo,2).then(data =>{
        service.queryCount().then(result=>{
            data.totalPage=Math.ceil(result[0].sl/2)
            data.total=result[0]
            res.json(data)
        })
    })
})   */

module.exports = (app) => {
  app.use(PATH, router)
}
